/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null, curr = head;
        while(curr != null){
            Listnode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
        
        /* if (head==null || head.next == null)
            return head;
        ListNode p = null, q = head, r = head.next;
        q.next = p;
        while (r != null){
            p = q;
            q = r;
            r = r.next;
            q.next = p;
        }
        return q;*/
    }
}